﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mdryden.Core.Localization;
using LastoRevival.Website.Settings;
using AttributeRouting.Web.Mvc;

namespace LastoRevival.Website.Controllers
{
	public class ForumController : LastoControllerBase
	{


		[GET("")]
		public ViewResult Index()
		{
			var forums = Service.GetVisibleForums(LoginState.Token);

			return View("Index", forums);
		}

		[GET("forum/{forumId}")]
		public ActionResult ViewForum(int forumID)
		{
			return ViewForumPage(forumID, 1);
		}

		[GET("forum/{forumId}/{page}")]
		public ActionResult ViewForumPage(int forumID, int page)
		{
			var pageSize = WebsiteSettings.GetInstance().ThreadsPerPage;

			var forum = Service.GetForumAndThreads(LoginState.Token, forumID, page, pageSize);

			// the user was screwing with page numbers, send them to page 1
			if (forum.Threads.Count() == 0 && forum.ForumDetails.ThreadCount != 0)
			{
				return RedirectToAction("ViewForum", new { forumID = forumID });
			}

			ViewBag.OnlineUsers = Service.GetOnlineUsers(LoginState.Token);

			return View("Forum", forum);
		}

		//[GET("forum/{forumId}/post")]
		//public ViewResult Post(int forumID)
		//{
		//    ViewData["ForumID"] = forumID;

		//    return View("Post");
		//}

		public ViewResult Search(string query, int? page)
		{
			// the google control on the page handles the results
			return View("SearchResults");
		}
	}
}
